<?php
	function validateEmail($email){  
        return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email);
    } 
	
	function validateName($name, $surname){
	       $length=strlen($name) > 1;
		   $length2=strlen($surname) > 1;
		   return($length && $length2);
	}
	
	
	function validatePasswords($pass1, $pass2) {  
        //if DOESN'T MATCH  
        if(strpos($pass1, ' ') !== false)  
            return false;  
        //if are valid  
        
        $length=strlen($pass1) > 4;
        $equal=$pass1 == $pass2;
        
		
        return ($equal && $length);
    }  
	
	class register extends BaseController{
		public function getData($in_params){
			$this->template_data['error']=(isset($in_params['action']) && $in_params['action']=='register')?1:0;
			
			return;
		}
		
		
		public function action_register($in_params=array()){
			//$in_params['email']
			
			$apply=new m_apply();
			$apply->email=$in_params['email'];
			$apply->name=$in_params['name'];
			$apply->surname=$in_params['surname'];
			
			$pass1=$in_params['password'];
			$pass2=$in_params['password2'];
			
			
			if ((validateEmail($apply->email))&&(validatePasswords($pass1, $pass2)) && (validateName($apply->name, $apply->surname))) {
					
				
				$apply->password=md5($in_params['password']);
				
				
				if ($apply->insert()){
					general::redirect('login?registered=1');
				} else {
					$this->template_data['error']='1';
				}

			} else {

				if(!validateEmail($apply->email)) {
					$this->template_data['error']='2';
				}
				
				if($pass1==$pass2) {
					$this->template_data['error']='3';
				}
				
				if(strlen($pass1)<6){
					$this->template_data['error']='4';
				}
			}
			
		}
		
	}
?>